package control;

import java.io.IOException;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2.ejb.entity.Lavoro;
import swimv2.ejb.remote.GestioneLavoroRemote;
import swimv2.ejb.util.LoginToken;
import util.HtmlUtil;

/**
 * Servlet implementation class PaginaDipendente
 */
public class PaginaDipendente extends Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PaginaDipendente() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		showElencoLavoriDipendente(request,response);
	}

	private void showElencoLavoriDipendente(HttpServletRequest request,
			HttpServletResponse response) {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneLavoro/remote");
			GestioneLavoroRemote gestioneLavoroRemote =(GestioneLavoroRemote) PortableRemoteObject.narrow(ref1, GestioneLavoroRemote.class);

			List<Lavoro> richiesteInviate = gestioneLavoroRemote.getRichiesteInviate(username);
			request.setAttribute("richiesteInviate", richiesteInviate);

			List<Lavoro> lavoroAttuale = gestioneLavoroRemote.getPropriLavori(username);
			request.setAttribute("lavoroAttuale", lavoroAttuale);
			
			List<Lavoro> lavoriSvolti = gestioneLavoroRemote.getLavoriSvolti(username);
			request.setAttribute("lavoriSvolti", lavoriSvolti);
			
			
			
			
			
			HtmlUtil.CompletaEdIncludi(request, response, "/WEB-INF/pages/elencoLavoriDipendente.jsp");
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String azione=(String) request.getParameter("azione");
		if(azione.equals("concludiLavoro"))
		{
			
		}
		concludiLavoro(request,response);	

	}
	
	private void concludiLavoro(HttpServletRequest request,HttpServletResponse response) {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");

		String username=tok.getUsername();

	
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneLavoro/remote");
			GestioneLavoroRemote gestioneLavoroRemote =(GestioneLavoroRemote) PortableRemoteObject.narrow(ref1, GestioneLavoroRemote.class);
			int id = Integer.parseInt(request.getParameter("id"));
			gestioneLavoroRemote.concludiLavoro(username,id);
						
			HtmlUtil.redirect(request, response, "/PaginaDipendente");
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
		}	
		
	}

}
